package com.tensquare.article.controller;


import com.tensquare.article.pojo.Comment;
import com.tensquare.article.service.CommenntService;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: xxx
 * @Date: 2022/10/26 21:05
 * @Description: 评论控制层
 */
@RestController
@RequestMapping("/comment")
public class CommenntController {

    @Autowired
    private CommenntService commenntService;

    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "根据评论id点赞评论")
    @PutMapping("/thumbup/{commentId}")
    public Result thumbup(@PathVariable String commentId) {
        //把用户点赞信息保存在redis中
        //每次点赞之前，先查询用户用户点赞信息
        //如果没有点赞信息，用户可以点赞
        //如果又点赞信息，用户不能重复点赞


        //模拟用户id
        String userId = "123";

        //先查询用户点赞信息，根据用户id和评论id
        Object like = redisTemplate.opsForValue().get("thumbup_" + userId + "_" + commentId);

        //判断查询到的结果是否为null
        if (like == null) {
            //如果为空，表示用户没有点过赞，可以点赞
            commenntService.thumbupLike(commentId);
            //点赞成功，保存点赞信息
            redisTemplate.opsForValue().set("thumbup_" + userId + "_" + commentId, 1);
            return new Result(true, StatusCode.OK, "点赞评论成功", null);

        }else{
            //取消点赞
            commenntService.thumbupDisLike(commentId);
            redisTemplate.delete("thumbup_" + userId + "_" + commentId);
            //如果不为空，表示用户点过赞，不可以重复点赞
            return new Result(false, StatusCode.ERROR, "重复/取消点赞", null);
        }
    }


    @ApiOperation(value = "根据文章的id查询文章评论")
    @GetMapping("/article/{articleId}")
    public Result findByarticleId(@PathVariable String articleId) {
        List<Comment> list = commenntService.findByArticleId(articleId);
        return new Result(true, StatusCode.OK, "查询文章评论成功", list);
    }


    @ApiOperation(value = "根据评论id删除评论")
    @DeleteMapping("/{commentId}")
    public Result deleteByCommentId(@PathVariable String commentId) {
        //执行修改
        commenntService.deleteByCommentId(commentId);
        return new Result(true, StatusCode.OK, "删除评论成功", null);
    }

    @ApiOperation(value = "根据评论id修改评论")
    @PutMapping("/{commentId}")
    public Result updateByCommentId(@PathVariable String commentId, @RequestBody Comment comment) {
        //设置评论主键
        comment.set_id(commentId);
        //执行修改
        commenntService.updateByCommentId(comment);
        return new Result(true, StatusCode.OK, "修改评论成功", null);
    }

    @ApiOperation(value = "新增评论")
    @PostMapping()
    public Result save(@RequestBody Comment comment) {
        comment = commenntService.save(comment);
        return new Result(true, StatusCode.OK, "新增评论成功", comment);
    }

    @ApiOperation(value = "根据评论id查询评论数据")
    @GetMapping("/{commentId}")
    public Result findByCommentId(@PathVariable String commentId) {
        Comment comment = commenntService.findByCommentId(commentId);
        return new Result(true, StatusCode.OK, "查询成功", comment);
    }

    @ApiOperation(value = "查询所有评论")
    @GetMapping()
    public Result findAll() {
        List<Comment> commentList = commenntService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", commentList);
    }


}    